

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.compression.quant.qat &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../api_python/mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../../index.html">Module code</a> &raquo;</li>
        
      <li>mindspore.compression.quant.qat</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for mindspore.compression.quant.qat</h1><div class="highlight"><pre>
<span></span><span class="c1"># Copyright 2020 Huawei Technologies Co., Ltd</span>
<span class="c1">#</span>
<span class="c1"># Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1"># you may not use this file except in compliance with the License.</span>
<span class="c1"># You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1"># ============================================================================</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Quantization aware training</span>

<span class="sd">User can use quantization aware to train a model. MindSpore supports quantization aware training,</span>
<span class="sd">which models quantization errors in both the forward and backward passes using fake-quantization</span>
<span class="sd">operations. Note that the entire computation is carried out in floating point. At the end of quantization</span>
<span class="sd">aware training, MindSpore provides conversion functions to convert the trained model into lower precision.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">mindspore.context</span> <span class="k">as</span> <span class="nn">context</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">...</span> <span class="kn">import</span> <span class="n">nn</span><span class="p">,</span> <span class="n">ops</span>
<span class="kn">from</span> <span class="nn">..._checkparam</span> <span class="kn">import</span> <span class="n">Validator</span><span class="p">,</span> <span class="n">Rel</span>
<span class="kn">from</span> <span class="nn">...nn.layer</span> <span class="kn">import</span> <span class="n">quant</span>
<span class="kn">from</span> <span class="nn">...ops</span> <span class="kn">import</span> <span class="n">functional</span> <span class="k">as</span> <span class="n">F</span>
<span class="kn">from</span> <span class="nn">..common</span> <span class="kn">import</span> <span class="n">QuantDtype</span>
<span class="kn">from</span> <span class="nn">.quantizer</span> <span class="kn">import</span> <span class="n">Quantizer</span><span class="p">,</span> <span class="n">OptimizeOption</span>
<span class="kn">from</span> <span class="nn">.quant_utils</span> <span class="kn">import</span> <span class="n">compute_kl_threshold</span>


<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;QuantizationAwareTraining&quot;</span><span class="p">,</span> <span class="s2">&quot;create_quant_config&quot;</span><span class="p">]</span>


<div class="viewcode-block" id="create_quant_config"><a class="viewcode-back" href="../../../../api_python/mindspore.compression.html#mindspore.compression.quant.create_quant_config">[docs]</a><span class="k">def</span> <span class="nf">create_quant_config</span><span class="p">(</span><span class="n">quant_observer</span><span class="o">=</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">FakeQuantWithMinMaxObserver</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">FakeQuantWithMinMaxObserver</span><span class="p">),</span>
                        <span class="n">quant_delay</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                        <span class="n">quant_dtype</span><span class="o">=</span><span class="p">(</span><span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT8</span><span class="p">,</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT8</span><span class="p">),</span>
                        <span class="n">per_channel</span><span class="o">=</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span>
                        <span class="n">symmetric</span><span class="o">=</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span>
                        <span class="n">narrow_range</span><span class="o">=</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span>
                        <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;DEFAULT&quot;</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Config the observer type of weights and data flow with quant parameters.</span>

<span class="sd">    Args:</span>
<span class="sd">        quant_observer (Union[Observer, list, tuple]): The types of observer for quantization. The first element</span>
<span class="sd">            applies to weights and the second applies to data flow. Currently, only</span>
<span class="sd">            :class:`FakeQuantWithMinMaxObserver` supported.</span>
<span class="sd">            Default: (nn.FakeQuantWithMinMaxObserver, nn.FakeQuantWithMinMaxObserver).</span>
<span class="sd">        quant_delay (Union[int, list, tuple]): Number of steps after which weights and activations are quantized</span>
<span class="sd">            during train and eval. The first element represents weights and the second element represents data flow.</span>
<span class="sd">            Default: (0, 0).</span>
<span class="sd">        quant_dtype (Union[QuantDtype, list, tuple]): Datatype used to quantize weights and activations. The first</span>
<span class="sd">            element represents weights and the second element represents data flow.</span>
<span class="sd">            Default: (QuantDtype.INT8, QuantDtype.INT8).</span>
<span class="sd">        per_channel (Union[bool, list, tuple]):  Quantization granularity based on layer or on channel. If `True`</span>
<span class="sd">            then base on per channel, otherwise base on per layer. The first element represents weights</span>
<span class="sd">            and the second element represents data flow, and the second element must be `False` now.</span>
<span class="sd">            Default: (False, False).</span>
<span class="sd">        symmetric (Union[bool, list, tuple]): Whether the quantization algorithm is symmetric or not. If `True` then</span>
<span class="sd">            base on symmetric, otherwise base on asymmetric. The first element represents weights and the second</span>
<span class="sd">            element represents data flow. Default: (False, False).</span>
<span class="sd">        narrow_range (Union[bool, list, tuple]): Whether the quantization algorithm uses narrow range or not.</span>
<span class="sd">            The first element represents weights and the second element represents data flow.</span>
<span class="sd">            Default: (False, False).</span>
<span class="sd">        mode (str): Optional quantization mode, currently only `DEFAULT`(QAT) and `LEARNED_SCALE` are supported.</span>
<span class="sd">            Default: &quot;DEFAULT&quot;.</span>

<span class="sd">    Returns:</span>
<span class="sd">        QuantConfig, contains the observer type of weight and activation.</span>

<span class="sd">    Raises:</span>
<span class="sd">        ValueError: If the second element of `per_channel` is not `False`.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">per_channel</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Arg &#39;per_channel&#39; second element must be &#39;False&#39;.&quot;</span><span class="p">)</span>
    <span class="n">weight_observer</span> <span class="o">=</span> <span class="n">quant_observer</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">partial_init</span><span class="p">(</span><span class="n">quant_delay</span><span class="o">=</span><span class="n">quant_delay</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">quant_dtype</span><span class="o">=</span><span class="n">quant_dtype</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                                     <span class="n">per_channel</span><span class="o">=</span><span class="n">per_channel</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">symmetric</span><span class="o">=</span><span class="n">symmetric</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                                                     <span class="n">narrow_range</span><span class="o">=</span><span class="n">narrow_range</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">)</span>
    <span class="n">act_observer</span> <span class="o">=</span> <span class="n">quant_observer</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">partial_init</span><span class="p">(</span><span class="n">quant_delay</span><span class="o">=</span><span class="n">quant_delay</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">quant_dtype</span><span class="o">=</span><span class="n">quant_dtype</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span>
                                                   <span class="n">per_channel</span><span class="o">=</span><span class="n">per_channel</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">symmetric</span><span class="o">=</span><span class="n">symmetric</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span>
                                                   <span class="n">narrow_range</span><span class="o">=</span><span class="n">narrow_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">mode</span><span class="o">=</span><span class="n">mode</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">quant</span><span class="o">.</span><span class="n">QuantConfig</span><span class="p">(</span><span class="n">weight</span><span class="o">=</span><span class="n">weight_observer</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="n">act_observer</span><span class="p">)</span></div>


<span class="k">class</span> <span class="nc">_AddFakeQuantInput</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Add FakeQuant OP at input of the network. Only support one input case.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">network</span><span class="p">,</span> <span class="n">quant_delay</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">_AddFakeQuantInput</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">auto_prefix</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fake_quant_input</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">FakeQuantWithMinMaxObserver</span><span class="p">(</span><span class="n">min_init</span><span class="o">=-</span><span class="mi">6</span><span class="p">,</span> <span class="n">max_init</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span>
                                                                  <span class="n">quant_delay</span><span class="o">=</span><span class="n">quant_delay</span><span class="p">,</span> <span class="n">ema</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fake_quant_input</span><span class="o">.</span><span class="n">update_parameters_name</span><span class="p">(</span><span class="s1">&#39;fake_quant_input.&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">network</span> <span class="o">=</span> <span class="n">network</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fake_quant_input</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">network</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span>


<span class="k">class</span> <span class="nc">_AddFakeQuantAfterSubCell</span><span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Cell</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Add FakeQuant OP after of the sub Cell.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subcell</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">_AddFakeQuantAfterSubCell</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">auto_prefix</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">subcell</span> <span class="o">=</span> <span class="n">subcell</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="s2">&quot;DEFAULT&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_init</span> <span class="o">=</span> <span class="mi">6</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_init</span> <span class="o">=</span> <span class="o">-</span><span class="mi">6</span>

        <span class="k">if</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">LEARNED_SCALE</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;optimize_option&quot;</span><span class="p">]:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="s2">&quot;LEARNED_SCALE&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">max_init</span> <span class="o">=</span> <span class="mi">16</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_init</span> <span class="o">=</span> <span class="o">-</span><span class="mi">16</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">fake_quant_act</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">FakeQuantWithMinMaxObserver</span><span class="p">(</span><span class="n">min_init</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">min_init</span><span class="p">,</span>
                                                                <span class="n">max_init</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">max_init</span><span class="p">,</span>
                                                                <span class="n">ema</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                                                <span class="n">quant_dtype</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;quant_dtype&quot;</span><span class="p">],</span>
                                                                <span class="n">quant_delay</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;quant_delay&quot;</span><span class="p">],</span>
                                                                <span class="n">per_channel</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;per_channel&quot;</span><span class="p">],</span>
                                                                <span class="n">symmetric</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;symmetric&quot;</span><span class="p">],</span>
                                                                <span class="n">narrow_range</span><span class="o">=</span><span class="n">kwargs</span><span class="p">[</span><span class="s2">&quot;narrow_range&quot;</span><span class="p">],</span>
                                                                <span class="n">mode</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mode</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">construct</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">data</span><span class="p">):</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">subcell</span><span class="p">(</span><span class="o">*</span><span class="n">data</span><span class="p">)</span>
        <span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fake_quant_act</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">output</span>


<div class="viewcode-block" id="QuantizationAwareTraining"><a class="viewcode-back" href="../../../../api_python/mindspore.compression.html#mindspore.compression.quant.QuantizationAwareTraining">[docs]</a><span class="k">class</span> <span class="nc">QuantizationAwareTraining</span><span class="p">(</span><span class="n">Quantizer</span><span class="p">):</span>
    <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Quantizer for quantization aware training.</span>

<span class="sd">    Args:</span>
<span class="sd">        bn_fold (bool): Whether to use bn fold ops for simulation inference operation. Default: True.</span>
<span class="sd">        freeze_bn (int): Number of steps after which BatchNorm OP parameters fixed to global mean and variance.</span>
<span class="sd">            Default: 1e7.</span>
<span class="sd">        quant_delay (Union[int, list, tuple]): Number of steps after which weights and activations are quantized</span>
<span class="sd">            during train and eval. The first element represents weights and the second element represents data flow.</span>
<span class="sd">            Default: (0, 0).</span>
<span class="sd">        quant_dtype (Union[QuantDtype, list, tuple]): Datatype used to quantize weights and activations. The first</span>
<span class="sd">            element represents weights and the second element represents data flow. It is necessary to consider the</span>
<span class="sd">            precision support of hardware devices in the practical quantization infer scenario.</span>
<span class="sd">            Default: (QuantDtype.INT8, QuantDtype.INT8).</span>
<span class="sd">        per_channel (Union[bool, list, tuple]):  Quantization granularity based on layer or on channel. If `True`</span>
<span class="sd">            then base on per channel, otherwise base on per layer. The first element represents weights and the</span>
<span class="sd">            second element represents data flow, and the second element must be `False` now. Default: (False, False).</span>
<span class="sd">        symmetric (Union[bool, list, tuple]): Whether the quantization algorithm is symmetric or not. If `True` then</span>
<span class="sd">            base on symmetric, otherwise base on asymmetric. The first element represents weights and the second</span>
<span class="sd">            element represents data flow. Default: (False, False).</span>
<span class="sd">        narrow_range (Union[bool, list, tuple]): Whether the quantization algorithm uses narrow range or not.</span>
<span class="sd">            The first element represents weights and the second element represents data flow.</span>
<span class="sd">            Default: (False, False).</span>
<span class="sd">        optimize_option (Union[OptimizeOption, list, tuple]): Specifies the quant algorithm and options, currently</span>
<span class="sd">            only support `QAT` and `LEARNED_SCALE` (Note that, if both `QAT` and `LEARNED_SCALE` are configured,</span>
<span class="sd">            `LEARNED_SCALE` has a higher priority. `LEARNED_SCALE` currently only work under some constraints, which</span>
<span class="sd">            includes: freeze_bn=0, quant_delay=0, symmetric=True, narrow_range=True, More specifically, for operators</span>
<span class="sd">            such as Relu and Relu6, which only have positive values, we add a negative truncation to optimize this</span>
<span class="sd">            scenario, and narrow_range will automatically match to False). Default: OptimizeOption.QAT.</span>
<span class="sd">        one_conv_fold (bool): Whether to use one conv bn fold ops for simulation inference operation. Default: True.</span>

<span class="sd">    Raises:</span>
<span class="sd">        TypeError: If the element of `quant_delay` or `freeze_bn` is not int.</span>
<span class="sd">        TypeError: If `bn_fold`, `one_conv_fold` or the element of `per_channel`, `symmetric`, `narrow_range`</span>
<span class="sd">            is not bool.</span>
<span class="sd">        TypeError: If the element of `quant_dtype` is not `QuantDtype`.</span>
<span class="sd">        ValueError: If the length of `quant_delay`, `quant_dtype`, `per_channel`, `symmetric` or `narrow_range` is</span>
<span class="sd">            not less than 2.</span>
<span class="sd">        ValueError: If the `optimize_option` is `LEARNED_SCALE` and `freeze_bn` is not equal to 0.</span>
<span class="sd">        ValueError: If the `optimize_option` is `LEARNED_SCALE` and `symmetric` is not (True, True).</span>
<span class="sd">        ValueError: If the `optimize_option` is `LEARNED_SCALE` and `narrow_range` is not (True, True).</span>
<span class="sd">        ValueError: If the `optimize_option` is `LEARNED_SCALE` and `quant_delay` is not (0, 0).</span>

<span class="sd">    Examples:</span>
<span class="sd">        &gt;&gt;&gt; from mindspore.compression.quant import QuantizationAwareTraining</span>
<span class="sd">        &gt;&gt;&gt; class LeNet5(nn.Cell):</span>
<span class="sd">        ...     def __init__(self, num_class=10, channel=1):</span>
<span class="sd">        ...         super(LeNet5, self).__init__()</span>
<span class="sd">        ...         self.type = &quot;fusion&quot;</span>
<span class="sd">        ...         self.num_class = num_class</span>
<span class="sd">        ...</span>
<span class="sd">        ...         # change `nn.Conv2d` to `nn.Conv2dBnAct`</span>
<span class="sd">        ...         self.conv1 = nn.Conv2dBnAct(channel, 6, 5, pad_mode=&#39;valid&#39;, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.conv2 = nn.Conv2dBnAct(6, 16, 5, pad_mode=&#39;valid&#39;, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         # change `nn.Dense` to `nn.DenseBnAct`</span>
<span class="sd">        ...         self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.fc2 = nn.DenseBnAct(120, 84, activation=&#39;relu&#39;)</span>
<span class="sd">        ...         self.fc3 = nn.DenseBnAct(84, self.num_class)</span>
<span class="sd">        ...</span>
<span class="sd">        ...         self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)</span>
<span class="sd">        ...         self.flatten = nn.Flatten()</span>
<span class="sd">        ...</span>
<span class="sd">        ...     def construct(self, x):</span>
<span class="sd">        ...         x = self.conv1(x)</span>
<span class="sd">        ...         x = self.max_pool2d(x)</span>
<span class="sd">        ...         x = self.conv2(x)</span>
<span class="sd">        ...         x = self.max_pool2d(x)</span>
<span class="sd">        ...         x = self.flatten(x)</span>
<span class="sd">        ...         x = self.fc1(x)</span>
<span class="sd">        ...         x = self.fc2(x)</span>
<span class="sd">        ...         x = self.fc3(x)</span>
<span class="sd">        ...         return x</span>
<span class="sd">        ...</span>
<span class="sd">        &gt;&gt;&gt; net = LeNet5()</span>
<span class="sd">        &gt;&gt;&gt; quantizer = QuantizationAwareTraining(bn_fold=False, per_channel=[True, False], symmetric=[True, False])</span>
<span class="sd">        &gt;&gt;&gt; net_qat = quantizer.quantize(net)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">__quant_op_name__</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Add&quot;</span><span class="p">,</span> <span class="s2">&quot;Sub&quot;</span><span class="p">,</span> <span class="s2">&quot;Mul&quot;</span><span class="p">,</span> <span class="s2">&quot;RealDiv&quot;</span><span class="p">,</span> <span class="s2">&quot;ReduceMean&quot;</span><span class="p">]</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span>
                 <span class="n">bn_fold</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                 <span class="n">freeze_bn</span><span class="o">=</span><span class="mi">10000000</span><span class="p">,</span>
                 <span class="n">quant_delay</span><span class="o">=</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span>
                 <span class="n">quant_dtype</span><span class="o">=</span><span class="p">(</span><span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT8</span><span class="p">,</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT8</span><span class="p">),</span>
                 <span class="n">per_channel</span><span class="o">=</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span>
                 <span class="n">symmetric</span><span class="o">=</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span>
                 <span class="n">narrow_range</span><span class="o">=</span><span class="p">(</span><span class="kc">False</span><span class="p">,</span> <span class="kc">False</span><span class="p">),</span>
                 <span class="n">optimize_option</span><span class="o">=</span><span class="n">OptimizeOption</span><span class="o">.</span><span class="n">QAT</span><span class="p">,</span>
                 <span class="n">one_conv_fold</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Init for QuantizationAwareTraining quantizer&quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">QuantizationAwareTraining</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">optimize_option</span><span class="o">=</span><span class="n">optimize_option</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">convert2list</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">):</span>
                <span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">value</span><span class="p">]</span>
            <span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">2</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;input `</span><span class="si">{}</span><span class="s2">` len should less then 2&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">name</span><span class="p">))</span>
            <span class="k">return</span> <span class="n">value</span>

        <span class="n">quant_delay</span> <span class="o">=</span> <span class="n">convert2list</span><span class="p">(</span><span class="s2">&quot;quant delay&quot;</span><span class="p">,</span> <span class="n">quant_delay</span><span class="p">)</span>
        <span class="n">quant_dtype</span> <span class="o">=</span> <span class="n">convert2list</span><span class="p">(</span><span class="s2">&quot;quant dtype&quot;</span><span class="p">,</span> <span class="n">quant_dtype</span><span class="p">)</span>
        <span class="n">per_channel</span> <span class="o">=</span> <span class="n">convert2list</span><span class="p">(</span><span class="s2">&quot;per channel&quot;</span><span class="p">,</span> <span class="n">per_channel</span><span class="p">)</span>
        <span class="n">symmetric</span> <span class="o">=</span> <span class="n">convert2list</span><span class="p">(</span><span class="s2">&quot;symmetric&quot;</span><span class="p">,</span> <span class="n">symmetric</span><span class="p">)</span>
        <span class="n">narrow_range</span> <span class="o">=</span> <span class="n">convert2list</span><span class="p">(</span><span class="s2">&quot;narrow range&quot;</span><span class="p">,</span> <span class="n">narrow_range</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">weight_qdelay</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_non_negative_int</span><span class="p">(</span><span class="n">quant_delay</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;quant delay&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">act_qdelay</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_int</span><span class="p">(</span><span class="n">quant_delay</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="mi">0</span><span class="p">,</span> <span class="n">Rel</span><span class="o">.</span><span class="n">GE</span><span class="p">,</span> <span class="s2">&quot;quant delay&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bn_fold</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">bn_fold</span><span class="p">,</span> <span class="s2">&quot;bn fold&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">freeze_bn</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_non_negative_int</span><span class="p">(</span><span class="n">freeze_bn</span><span class="p">,</span> <span class="s2">&quot;freeze bn&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_isinstance</span><span class="p">(</span><span class="s2">&quot;weights dtype&quot;</span><span class="p">,</span> <span class="n">quant_dtype</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">QuantDtype</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">act_dtype</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_isinstance</span><span class="p">(</span><span class="s2">&quot;activations dtype&quot;</span><span class="p">,</span> <span class="n">quant_dtype</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="n">QuantDtype</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight_channel</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">per_channel</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;per channel&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">act_channel</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">per_channel</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;per channel&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight_symmetric</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">symmetric</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;symmetric&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">act_symmetric</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">symmetric</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;symmetric&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">weight_range</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">narrow_range</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;narrow range&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">act_range</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">narrow_range</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;narrow range&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">one_conv_fold</span> <span class="o">=</span> <span class="n">Validator</span><span class="o">.</span><span class="n">check_bool</span><span class="p">(</span><span class="n">one_conv_fold</span><span class="p">,</span> <span class="s2">&quot;one conv fold&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_convert_method_map</span> <span class="o">=</span> <span class="p">{</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnAct</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_conv</span><span class="p">,</span>
                                    <span class="n">nn</span><span class="o">.</span><span class="n">DenseBnAct</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_dense</span><span class="p">}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="s2">&quot;DEFAULT&quot;</span>
        <span class="k">if</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">LEARNED_SCALE</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">mode</span> <span class="o">=</span> <span class="s2">&quot;LEARNED_SCALE&quot;</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_symmetric</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">act_symmetric</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;OptimizeOption.LEARNED_SCALE currently only support &quot;</span>
                                 <span class="s2">&quot;symmetric=(True, True) for quant&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_range</span> <span class="ow">or</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">act_range</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;OptimizeOption.LEARNED_SCALE currently only support narrow_range=(True, True) &quot;</span>
                                 <span class="s2">&quot;for quant&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">freeze_bn</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;OptimizeOption.LEARNED_SCALE currently only support freeze_bn equal to 0, &quot;</span>
                                 <span class="s2">&quot;but get freeze_bn=</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">freeze_bn</span><span class="p">))</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_qdelay</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="bp">self</span><span class="o">.</span><span class="n">act_qdelay</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;OptimizeOption.LEARNED_SCALE currently only support quant_delay=(0, 0)&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span> <span class="o">=</span> <span class="n">create_quant_config</span><span class="p">(</span><span class="n">quant_delay</span><span class="o">=</span><span class="n">quant_delay</span><span class="p">,</span>
                                                <span class="n">quant_dtype</span><span class="o">=</span><span class="n">quant_dtype</span><span class="p">,</span>
                                                <span class="n">per_channel</span><span class="o">=</span><span class="n">per_channel</span><span class="p">,</span>
                                                <span class="n">symmetric</span><span class="o">=</span><span class="n">symmetric</span><span class="p">,</span>
                                                <span class="n">narrow_range</span><span class="o">=</span><span class="n">narrow_range</span><span class="p">,</span>
                                                <span class="n">mode</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">mode</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">eps</span> <span class="o">=</span> <span class="mf">1e-5</span>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">_convert_op_name</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
        <span class="n">pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;([A-Z]</span><span class="si">{1}</span><span class="s1">)&#39;</span><span class="p">)</span>
        <span class="n">name_new</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="sa">r</span><span class="s1">&#39;_\1&#39;</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">name_new</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;_&#39;</span><span class="p">:</span>
            <span class="n">name_new</span> <span class="o">=</span> <span class="n">name_new</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="k">return</span> <span class="n">name_new</span>

<div class="viewcode-block" id="QuantizationAwareTraining.quantize"><a class="viewcode-back" href="../../../../api_python/mindspore.compression.html#mindspore.compression.quant.QuantizationAwareTraining.quantize">[docs]</a>    <span class="k">def</span> <span class="nf">quantize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">network</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Quant API to convert input network to a quantization aware training network.</span>

<span class="sd">        Note:</span>
<span class="sd">            Please refer to the Examples of class: `mindspore.compression.quant.QuantizationAwareTraining`.</span>

<span class="sd">        Args:</span>
<span class="sd">            network (Cell): network to be quantized.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Cell, a quantization aware training network.</span>

<span class="sd">        Raises:</span>
<span class="sd">            KeyError: If the `device_target` set in context is not in `support_device`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">support_device</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;Ascend&quot;</span><span class="p">,</span> <span class="s2">&quot;GPU&quot;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">context</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s1">&#39;device_target&#39;</span><span class="p">)</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">support_device</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">KeyError</span><span class="p">(</span><span class="s2">&quot;Unsupported </span><span class="si">{}</span><span class="s2"> device target.&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">context</span><span class="o">.</span><span class="n">get_context</span><span class="p">(</span><span class="s1">&#39;device_target&#39;</span><span class="p">)))</span>

        <span class="k">if</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">QAT</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span> <span class="ow">or</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">LEARNED_SCALE</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">:</span>
            <span class="n">network</span><span class="o">.</span><span class="n">update_cell_prefix</span><span class="p">()</span>
            <span class="n">network</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_subcells2quant</span><span class="p">(</span><span class="n">network</span><span class="p">)</span>
            <span class="n">network</span><span class="o">.</span><span class="n">update_cell_type</span><span class="p">(</span><span class="s2">&quot;quant&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">network</span></div>

    <span class="k">def</span> <span class="nf">_convert_subcells2quant</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">network</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        convert sub cell like `Conv2dBnAct` and `DenseBnAct` to quant cell</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">cells</span> <span class="o">=</span> <span class="n">network</span><span class="o">.</span><span class="n">name_cells</span><span class="p">()</span>
        <span class="n">change</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">cells</span><span class="p">:</span>
            <span class="n">subcell</span> <span class="o">=</span> <span class="n">cells</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">subcell</span> <span class="o">==</span> <span class="n">network</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">subcell</span><span class="p">,</span> <span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnAct</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DenseBnAct</span><span class="p">)):</span>
                <span class="n">prefix</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">param_prefix</span>
                <span class="n">new_subcell</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_method_map</span><span class="p">[</span><span class="nb">type</span><span class="p">(</span><span class="n">subcell</span><span class="p">)](</span><span class="n">subcell</span><span class="p">)</span>
                <span class="n">new_subcell</span><span class="o">.</span><span class="n">update_parameters_name</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span><span class="p">)</span>
                <span class="n">network</span><span class="o">.</span><span class="n">insert_child_to_cell</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">new_subcell</span><span class="p">)</span>
                <span class="n">change</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_convert_subcells2quant</span><span class="p">(</span><span class="n">subcell</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">network</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">SequentialCell</span><span class="p">)</span> <span class="ow">and</span> <span class="n">change</span><span class="p">:</span>
            <span class="n">network</span><span class="o">.</span><span class="n">cell_list</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">network</span><span class="o">.</span><span class="n">cells</span><span class="p">())</span>

        <span class="c1"># add FakeQuant OP after OP in white list, but not including those wrapped in the below quantization cell.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">network</span><span class="p">,</span> <span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">FakeQuantWithMinMaxObserver</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnFoldQuantOneConv</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnFoldQuant</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnWithoutFoldQuant</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dQuant</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">DenseQuant</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">ActQuant</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">TensorAddQuant</span><span class="p">,</span>
                                <span class="n">nn</span><span class="o">.</span><span class="n">MulQuant</span><span class="p">)):</span>
            <span class="k">return</span> <span class="n">network</span>

        <span class="n">add_list</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">network</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">name</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;_&#39;</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="n">attr</span> <span class="o">=</span> <span class="n">network</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">attr</span><span class="p">,</span> <span class="n">ops</span><span class="o">.</span><span class="n">Primitive</span><span class="p">)</span> <span class="ow">and</span> <span class="n">attr</span><span class="o">.</span><span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">__quant_op_name__</span><span class="p">:</span>
                <span class="n">add_list</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">name</span><span class="p">,</span> <span class="n">attr</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">prim_op</span> <span class="ow">in</span> <span class="n">add_list</span><span class="p">:</span>
            <span class="n">prefix</span> <span class="o">=</span> <span class="n">name</span>
            <span class="n">add_quant</span> <span class="o">=</span> <span class="n">_AddFakeQuantAfterSubCell</span><span class="p">(</span><span class="n">prim_op</span><span class="p">,</span>
                                                  <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_dtype</span><span class="p">,</span>
                                                  <span class="n">quant_delay</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_qdelay</span><span class="p">,</span>
                                                  <span class="n">per_channel</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_channel</span><span class="p">,</span>
                                                  <span class="n">symmetric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_symmetric</span><span class="p">,</span>
                                                  <span class="n">narrow_range</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_range</span><span class="p">,</span>
                                                  <span class="n">optimize_option</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">network</span><span class="o">.</span><span class="n">param_prefix</span><span class="p">:</span>
                <span class="n">prefix</span> <span class="o">=</span> <span class="s1">&#39;.&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">network</span><span class="o">.</span><span class="n">param_prefix</span><span class="p">,</span> <span class="n">prefix</span><span class="p">])</span>
            <span class="n">add_quant</span><span class="o">.</span><span class="n">update_parameters_name</span><span class="p">(</span><span class="n">prefix</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span><span class="p">)</span>
            <span class="k">del</span> <span class="n">network</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="n">network</span><span class="o">.</span><span class="n">insert_child_to_cell</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">add_quant</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">network</span>

    <span class="k">def</span> <span class="nf">_convert_conv</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subcell</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        convert Conv2d cell to quant cell</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">min_init</span> <span class="o">=</span> <span class="o">-</span><span class="mi">6</span>
        <span class="n">max_init</span> <span class="o">=</span> <span class="mi">6</span>
        <span class="k">if</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">LEARNED_SCALE</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">:</span>
            <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">has_bn</span><span class="p">:</span>
                <span class="n">scale_factor</span> <span class="o">=</span> <span class="p">(</span><span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">/</span>
                                <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">))</span>
                <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell_weight_para</span> <span class="o">*</span> <span class="n">scale_factor</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kl_init</span><span class="p">(</span><span class="n">subcell_weight_para</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span>
            <span class="n">weight</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">partial_init</span><span class="p">(</span><span class="n">min_init</span><span class="o">=</span><span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span><span class="o">=</span><span class="n">max_init</span><span class="p">))</span>

        <span class="n">conv_inner</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">conv</span>
        <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">has_bn</span><span class="p">:</span>
            <span class="n">bn_inner</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">bn_fold</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">one_conv_fold</span><span class="p">:</span>
                    <span class="n">conv_inner</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">Conv2dBnFoldQuantOneConv</span><span class="p">(</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">in_channels</span><span class="p">,</span>
                                                                <span class="n">conv_inner</span><span class="o">.</span><span class="n">out_channels</span><span class="p">,</span>
                                                                <span class="n">kernel_size</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">kernel_size</span><span class="p">,</span>
                                                                <span class="n">stride</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">stride</span><span class="p">,</span>
                                                                <span class="n">pad_mode</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">pad_mode</span><span class="p">,</span>
                                                                <span class="n">padding</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">padding</span><span class="p">,</span>
                                                                <span class="n">dilation</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">dilation</span><span class="p">,</span>
                                                                <span class="n">group</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">group</span><span class="p">,</span>
                                                                <span class="n">eps</span><span class="o">=</span><span class="n">bn_inner</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span>
                                                                <span class="n">momentum</span><span class="o">=</span><span class="mi">1</span> <span class="o">-</span> <span class="n">bn_inner</span><span class="o">.</span><span class="n">momentum</span><span class="p">,</span>
                                                                <span class="n">has_bias</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">has_bias</span><span class="p">,</span>
                                                                <span class="n">bias_init</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">bias_init</span><span class="p">,</span>
                                                                <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                                                <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">,</span>
                                                                <span class="n">fake</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">conv_inner</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">Conv2dBnFoldQuant</span><span class="p">(</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">in_channels</span><span class="p">,</span>
                                                         <span class="n">conv_inner</span><span class="o">.</span><span class="n">out_channels</span><span class="p">,</span>
                                                         <span class="n">kernel_size</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">kernel_size</span><span class="p">,</span>
                                                         <span class="n">stride</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">stride</span><span class="p">,</span>
                                                         <span class="n">pad_mode</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">pad_mode</span><span class="p">,</span>
                                                         <span class="n">padding</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">padding</span><span class="p">,</span>
                                                         <span class="n">dilation</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">dilation</span><span class="p">,</span>
                                                         <span class="n">group</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">group</span><span class="p">,</span>
                                                         <span class="n">eps</span><span class="o">=</span><span class="n">bn_inner</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span>
                                                         <span class="n">momentum</span><span class="o">=</span><span class="mi">1</span> <span class="o">-</span> <span class="n">bn_inner</span><span class="o">.</span><span class="n">momentum</span><span class="p">,</span>
                                                         <span class="n">has_bias</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">has_bias</span><span class="p">,</span>
                                                         <span class="n">bias_init</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">bias_init</span><span class="p">,</span>
                                                         <span class="n">freeze_bn</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">freeze_bn</span><span class="p">,</span>
                                                         <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                                         <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">,</span>
                                                         <span class="n">fake</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
                <span class="c1"># change original network Batch Normalization OP parameters to quant network</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">gamma</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">gamma</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">beta</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">moving_mean</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_mean</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">moving_variance</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_variance</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">conv_inner</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">Conv2dBnWithoutFoldQuant</span><span class="p">(</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">in_channels</span><span class="p">,</span>
                                                            <span class="n">conv_inner</span><span class="o">.</span><span class="n">out_channels</span><span class="p">,</span>
                                                            <span class="n">kernel_size</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">kernel_size</span><span class="p">,</span>
                                                            <span class="n">stride</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">stride</span><span class="p">,</span>
                                                            <span class="n">pad_mode</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">pad_mode</span><span class="p">,</span>
                                                            <span class="n">padding</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">padding</span><span class="p">,</span>
                                                            <span class="n">dilation</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">dilation</span><span class="p">,</span>
                                                            <span class="n">group</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">group</span><span class="p">,</span>
                                                            <span class="n">eps</span><span class="o">=</span><span class="n">bn_inner</span><span class="o">.</span><span class="n">eps</span><span class="p">,</span>
                                                            <span class="n">momentum</span><span class="o">=</span><span class="mi">1</span> <span class="o">-</span> <span class="n">bn_inner</span><span class="o">.</span><span class="n">momentum</span><span class="p">,</span>
                                                            <span class="n">has_bias</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">has_bias</span><span class="p">,</span>
                                                            <span class="n">bias_init</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">bias_init</span><span class="p">,</span>
                                                            <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                                            <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
                <span class="c1"># change original network Batch Normalization OP parameters to quant network</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">gamma</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">gamma</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">beta</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">beta</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_mean</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_mean</span>
                <span class="n">conv_inner</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_variance</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_variance</span>
            <span class="k">del</span> <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">has_bn</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">conv_inner</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">Conv2dQuant</span><span class="p">(</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">in_channels</span><span class="p">,</span> <span class="n">conv_inner</span><span class="o">.</span><span class="n">out_channels</span><span class="p">,</span>
                                           <span class="n">kernel_size</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">kernel_size</span><span class="p">,</span> <span class="n">stride</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">stride</span><span class="p">,</span>
                                           <span class="n">pad_mode</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">pad_mode</span><span class="p">,</span> <span class="n">padding</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">padding</span><span class="p">,</span>
                                           <span class="n">dilation</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">dilation</span><span class="p">,</span> <span class="n">group</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">group</span><span class="p">,</span>
                                           <span class="n">has_bias</span><span class="o">=</span><span class="n">conv_inner</span><span class="o">.</span><span class="n">has_bias</span><span class="p">,</span> <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                           <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
        <span class="c1"># change original network Conv2D OP parameters to quant network</span>
        <span class="n">conv_inner</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">weight</span>
        <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">has_bias</span><span class="p">:</span>
            <span class="n">conv_inner</span><span class="o">.</span><span class="n">bias</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">bias</span>
        <span class="n">subcell</span><span class="o">.</span><span class="n">conv</span> <span class="o">=</span> <span class="n">conv_inner</span>
        <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">has_act</span> <span class="ow">and</span> <span class="n">subcell</span><span class="o">.</span><span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_activation</span><span class="p">(</span><span class="n">subcell</span><span class="o">.</span><span class="n">activation</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">subcell</span><span class="o">.</span><span class="n">after_fake</span><span class="p">:</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">has_act</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">_AddFakeQuantAfterSubCell</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">identity</span><span class="p">,</span> <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_dtype</span><span class="p">,</span>
                                                           <span class="n">quant_delay</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_qdelay</span><span class="p">,</span> <span class="n">per_channel</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_channel</span><span class="p">,</span>
                                                           <span class="n">symmetric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_symmetric</span><span class="p">,</span> <span class="n">narrow_range</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_range</span><span class="p">,</span>
                                                           <span class="n">optimize_option</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">subcell</span>

    <span class="k">def</span> <span class="nf">_convert_dense</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subcell</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        convert dense cell to quant cell</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">min_init</span> <span class="o">=</span> <span class="o">-</span><span class="mi">6</span>
        <span class="n">max_init</span> <span class="o">=</span> <span class="mi">6</span>
        <span class="k">if</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">LEARNED_SCALE</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">:</span>
            <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">has_bn</span><span class="p">:</span>
                <span class="n">scale_factor</span> <span class="o">=</span> <span class="p">(</span><span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">/</span>
                                <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">subcell</span><span class="o">.</span><span class="n">batchnorm</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">))</span>
                <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell_weight_para</span> <span class="o">*</span> <span class="n">scale_factor</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kl_init</span><span class="p">(</span><span class="n">subcell_weight_para</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span>
            <span class="n">weight</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">partial_init</span><span class="p">(</span><span class="n">min_init</span><span class="o">=</span><span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span><span class="o">=</span><span class="n">max_init</span><span class="p">))</span>

        <span class="n">dense_inner</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">dense</span>
        <span class="n">dense_inner</span> <span class="o">=</span> <span class="n">quant</span><span class="o">.</span><span class="n">DenseQuant</span><span class="p">(</span><span class="n">dense_inner</span><span class="o">.</span><span class="n">in_channels</span><span class="p">,</span>
                                       <span class="n">dense_inner</span><span class="o">.</span><span class="n">out_channels</span><span class="p">,</span>
                                       <span class="n">has_bias</span><span class="o">=</span><span class="n">dense_inner</span><span class="o">.</span><span class="n">has_bias</span><span class="p">,</span>
                                       <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                       <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
        <span class="c1"># change original network Dense OP parameters to quant network</span>
        <span class="n">dense_inner</span><span class="o">.</span><span class="n">weight</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">weight</span>
        <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">has_bias</span><span class="p">:</span>
            <span class="n">dense_inner</span><span class="o">.</span><span class="n">bias</span> <span class="o">=</span> <span class="n">subcell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">bias</span>
        <span class="n">subcell</span><span class="o">.</span><span class="n">dense</span> <span class="o">=</span> <span class="n">dense_inner</span>
        <span class="k">if</span> <span class="n">subcell</span><span class="o">.</span><span class="n">has_act</span> <span class="ow">and</span> <span class="n">subcell</span><span class="o">.</span><span class="n">activation</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_convert_activation</span><span class="p">(</span><span class="n">subcell</span><span class="o">.</span><span class="n">activation</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">subcell</span><span class="o">.</span><span class="n">after_fake</span><span class="p">:</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">has_act</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="n">subcell</span><span class="o">.</span><span class="n">activation</span> <span class="o">=</span> <span class="n">_AddFakeQuantAfterSubCell</span><span class="p">(</span><span class="n">F</span><span class="o">.</span><span class="n">identity</span><span class="p">,</span>
                                                           <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_dtype</span><span class="p">,</span>
                                                           <span class="n">quant_delay</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_qdelay</span><span class="p">,</span>
                                                           <span class="n">per_channel</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_channel</span><span class="p">,</span>
                                                           <span class="n">symmetric</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_symmetric</span><span class="p">,</span>
                                                           <span class="n">narrow_range</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_range</span><span class="p">,</span>
                                                           <span class="n">optimize_option</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">subcell</span>

    <span class="k">def</span> <span class="nf">_convert_activation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">activation</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        convert activation cell to quant cell</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">act_class</span> <span class="o">=</span> <span class="n">activation</span><span class="o">.</span><span class="vm">__class__</span>
        <span class="n">act_list</span> <span class="o">=</span> <span class="p">[</span><span class="n">nn</span><span class="o">.</span><span class="n">ReLU</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">ReLU6</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">Sigmoid</span><span class="p">]</span>
        <span class="n">act_list_with_fake_before</span> <span class="o">=</span> <span class="p">[</span><span class="n">nn</span><span class="o">.</span><span class="n">LeakyReLU</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">HSigmoid</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">HSwish</span><span class="p">]</span>

        <span class="k">if</span> <span class="n">act_class</span> <span class="ow">in</span> <span class="n">act_list</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">quant</span><span class="o">.</span><span class="n">ActQuant</span><span class="p">(</span><span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">,</span>
                                  <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                  <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_dtype</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">act_class</span> <span class="ow">in</span> <span class="n">act_list_with_fake_before</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">quant</span><span class="o">.</span><span class="n">ActQuant</span><span class="p">(</span><span class="n">activation</span><span class="o">=</span><span class="n">activation</span><span class="p">,</span>
                                  <span class="n">ema</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                  <span class="n">fake_before</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                                  <span class="n">quant_config</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">quant_config</span><span class="p">,</span>
                                  <span class="n">quant_dtype</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">act_dtype</span><span class="p">)</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Unsupported activation in auto quant: &quot;</span><span class="p">,</span> <span class="n">act_class</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_kl_init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subcell_weight_para</span><span class="p">,</span> <span class="n">weight_dtype</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Calculate the value of max_init and min_init with compute_kl_threshold.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">weight_channel</span><span class="p">:</span>
            <span class="n">max_init</span> <span class="o">=</span> <span class="p">[</span><span class="n">compute_kl_threshold</span><span class="p">(</span><span class="n">weight_para_each</span><span class="p">,</span> <span class="n">weight_dtype</span><span class="p">)</span>
                        <span class="k">for</span> <span class="n">weight_para_each</span> <span class="ow">in</span> <span class="n">subcell_weight_para</span><span class="p">]</span>
            <span class="n">min_init</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">max_init</span><span class="p">]</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">max_init</span> <span class="o">=</span> <span class="p">[</span><span class="n">compute_kl_threshold</span><span class="p">(</span><span class="n">subcell_weight_para</span><span class="p">,</span> <span class="n">weight_dtype</span><span class="p">)]</span>
            <span class="n">min_init</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">max_init</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span>

    <span class="k">def</span> <span class="nf">_set_mixed_bits</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">network</span><span class="p">,</span> <span class="n">strategy</span><span class="p">):</span>
        <span class="sa">r</span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Set network&#39;s quantization strategy, this function is currently only valid for `LEARNED_SCALE`</span>
<span class="sd">        optimize_option.</span>

<span class="sd">        Args:</span>
<span class="sd">            network (Cell): Input network.</span>
<span class="sd">            strategy (list): The quantization strategy for layers that need to be quantified (eg. [[8], [8],</span>
<span class="sd">                ..., [6], [4], [8]]), currently only the quant_dtype for weights of the dense layer and the</span>
<span class="sd">                convolution layer is supported.</span>

<span class="sd">        Returns:</span>
<span class="sd">            Cell, a network with mixed bit strategy configured.</span>

<span class="sd">        Raises:</span>
<span class="sd">            ValueError: If `OptimizeOption.LEARNED_SCALE` is not in `self.optimize_option`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">OptimizeOption</span><span class="o">.</span><span class="n">LEARNED_SCALE</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">optimize_option</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The `_set_mixed_bits` function is currently only valid for `LEARNED_SCALE` &quot;</span>
                             <span class="s2">&quot;optimize_option.&quot;</span><span class="p">)</span>

        <span class="n">quantizable_idx</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">pass_cell</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">cell_and_name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">network</span><span class="o">.</span><span class="n">cells_and_names</span><span class="p">()):</span>
            <span class="n">cell</span> <span class="o">=</span> <span class="n">cell_and_name</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnAct</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DenseBnAct</span><span class="p">))</span> <span class="ow">and</span> <span class="n">cell</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">pass_cell</span><span class="p">:</span>
                <span class="n">quantizable_idx</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">quantizable_idx</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">strategy</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;The dimension of quantifiable layers is not consistent with that of strategy.&quot;</span><span class="p">)</span>

        <span class="n">quantizable_layer_bit_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">idx</span><span class="p">:</span> <span class="n">bit</span> <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">bit</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">quantizable_idx</span><span class="p">,</span> <span class="n">strategy</span><span class="p">)}</span>
        <span class="n">type_map</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT2</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT2</span><span class="p">,</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT3</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT3</span><span class="p">,</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT4</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT4</span><span class="p">,</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT5</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT5</span><span class="p">,</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT6</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT6</span><span class="p">,</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT7</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT7</span><span class="p">,</span>
            <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT8</span><span class="o">.</span><span class="n">num_bits</span><span class="p">:</span> <span class="n">QuantDtype</span><span class="o">.</span><span class="n">INT8</span>
        <span class="p">}</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">cell_and_name</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">network</span><span class="o">.</span><span class="n">cells_and_names</span><span class="p">()):</span>
            <span class="n">cell</span> <span class="o">=</span> <span class="n">cell_and_name</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">i</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">quantizable_idx</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="p">(</span><span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnAct</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DenseBnAct</span><span class="p">)):</span>
                    <span class="n">cell</span><span class="o">.</span><span class="n">weight_dtype</span> <span class="o">=</span> <span class="n">type_map</span><span class="p">[</span><span class="n">quantizable_layer_bit_dict</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]]</span>
                    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">Conv2dBnAct</span><span class="p">):</span>
                        <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">cell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">conv</span><span class="p">,</span> <span class="s1">&#39;gamma&#39;</span><span class="p">):</span>
                            <span class="n">scale_factor</span> <span class="o">=</span> <span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">/</span>
                                            <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">))</span>
                            <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell_weight_para</span> <span class="o">*</span> <span class="n">scale_factor</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
                        <span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kl_init</span><span class="p">(</span><span class="n">subcell_weight_para</span><span class="p">,</span> <span class="n">cell</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
                        <span class="n">cell</span><span class="o">.</span><span class="n">conv</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">reset</span><span class="p">(</span><span class="n">quant_dtype</span><span class="o">=</span><span class="n">cell</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">,</span>
                                                          <span class="n">min_init</span><span class="o">=</span><span class="n">min_init</span><span class="p">,</span>
                                                          <span class="n">max_init</span><span class="o">=</span><span class="n">max_init</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">cell</span><span class="p">,</span> <span class="n">nn</span><span class="o">.</span><span class="n">DenseBnAct</span><span class="p">):</span>
                        <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">cell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">weight</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span>
                        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">dense</span><span class="p">,</span> <span class="s1">&#39;gamma&#39;</span><span class="p">):</span>
                            <span class="n">scale_factor</span> <span class="o">=</span> <span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">gamma</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">/</span>
                                            <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">cell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">moving_variance</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">asnumpy</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">eps</span><span class="p">))</span>
                            <span class="n">subcell_weight_para</span> <span class="o">=</span> <span class="n">subcell_weight_para</span> <span class="o">*</span> <span class="n">scale_factor</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
                        <span class="n">min_init</span><span class="p">,</span> <span class="n">max_init</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_kl_init</span><span class="p">(</span><span class="n">subcell_weight_para</span><span class="p">,</span> <span class="n">cell</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">)</span>
                        <span class="n">cell</span><span class="o">.</span><span class="n">dense</span><span class="o">.</span><span class="n">fake_quant_weight</span><span class="o">.</span><span class="n">reset</span><span class="p">(</span><span class="n">quant_dtype</span><span class="o">=</span><span class="n">cell</span><span class="o">.</span><span class="n">weight_dtype</span><span class="p">,</span>
                                                           <span class="n">min_init</span><span class="o">=</span><span class="n">min_init</span><span class="p">,</span>
                                                           <span class="n">max_init</span><span class="o">=</span><span class="n">max_init</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">network</span></div>
</pre></div>

           </div>
           
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>